/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.uml_generator.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

/**
 * Main GUI panel.
 */
@SuppressWarnings("serial")
public class FileSelectionPanel
    extends JPanel
    implements ActionListener
{

  /**
   * The file text field.
   */
  private JTextField tXMIPath;

  /**
   * The file chooser button.
   */
  private JButton bBrowseXMI;

  /**
   * The folder path.
   */
  private JTextField tFolderPath;

  /**
   * The folder browser button.
   */
  private JButton bBrowseFolder;



  /**
   * Constructor.
   */
  public FileSelectionPanel()
  {
    super();

    final GridBagLayout layout = new GridBagLayout();
    final GridBagConstraints constraints = new GridBagConstraints();
    this.setLayout(layout);

    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 5, 5, 5);
    this.add(this.createPanel(), constraints);

  }



  /**
   * Create the projects panel.
   * 
   * @return the panel.
   */
  private JPanel createPanel()
  {
    final JPanel panel = new JPanel();

    final GridBagLayout layout = new GridBagLayout();
    final GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    panel.setBorder(BorderFactory.createTitledBorder("Create the dao from an UML model"));

    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final URL url = loader.getResource("com/genia/toolbox/uml_generator/gui/image/16x16.gif");
    final ImageIcon icon = new ImageIcon(url);

    final JLabel lXMILabel = new JLabel("Please pick an xmi/zargo file :");
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    panel.add(lXMILabel, constraints);

    this.tXMIPath = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 50, 5, 5);
    panel.add(this.tXMIPath, constraints);

    this.bBrowseXMI = new JButton(icon);
    this.bBrowseXMI.addActionListener(this);
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = new Insets(0, 0, 5, 5);
    panel.add(this.bBrowseXMI, constraints);

    final JLabel lFolderLabel = new JLabel("Please pick an output folder :");
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    panel.add(lFolderLabel, constraints);

    this.tFolderPath = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 4;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 50, 5, 5);
    panel.add(this.tFolderPath, constraints);

    this.bBrowseFolder = new JButton(icon);
    this.bBrowseFolder.addActionListener(this);
    constraints.gridx = 2;
    constraints.gridy = 4;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = new Insets(0, 0, 5, 5);
    panel.add(this.bBrowseFolder, constraints);

    return panel;
  }



  /**
   * Select a file.
   * 
   * @return the selected file.
   */
  private File selectFile()
  {
    File file = null;
    final JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    final FileFilter filter = new FileFilter()
    {
      @Override
      public boolean accept(File f)
      {
        boolean result = false;
        if (f.isDirectory() || f.getName().endsWith(".xmi") || f.getName().endsWith(".zargo")) {
          result = true;
        }
        return result;
      }



      @Override
      public String getDescription()
      {
        return ".xmi/.zargo";
      }
    };
    chooser.setFileFilter(filter);
    final int option = chooser.showOpenDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
      file = chooser.getSelectedFile();
    }
    return file;
  }



  /**
   * Select a folder.
   * 
   * @return the selected folder.
   */
  private File selectFolder()
  {
    File file = null;
    final JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    final int option = chooser.showOpenDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
      file = chooser.getSelectedFile();
    }
    return file;
  }



  /**
   * Get the selected XMI file.
   * 
   * @return the selected XML file.
   */
  public File getXMIFile()
  {
    File file = null;
    final String path = this.tXMIPath.getText();
    if (path != null && path.length() > 0) {
      file = new File(path);
    }
    return file;
  }



  /**
   * Get the selected output folder.
   * 
   * @return the selected output folder.
   */
  public File getOutputFolder()
  {
    File file = null;
    final String path = this.tFolderPath.getText();
    if (path != null && path.length() > 0) {
      file = new File(path);
    }
    return file;
  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          The action event.
   */
  public void actionPerformed(final ActionEvent e)
  {
    final Object source = e.getSource();
    if (source.equals(this.bBrowseXMI)) {
      final File file = this.selectFile();
      if (file != null) {
        this.tXMIPath.setText(file.getAbsolutePath());
      }
    }
    else if (source.equals(this.bBrowseFolder)) {
      final File file = this.selectFolder();
      if (file != null) {
        this.tFolderPath.setText(file.getAbsolutePath());
      }
    }
  }

}
